// 此文件放全局共用的数据
import { defineStore } from 'pinia'
import { ElNotification } from 'element-plus'
import router from '@app/base/router'
import { getFrontCallConfig } from '@/app/call-center/store/modules/client-setting'
export const globalModule = defineStore('global', {
  state: () => {
    return {
      audioObj: { audioUrl: '', currentId: '' }
      // audioUrl 音频地址  currentId一个列表多个录音的时候，需要根据id来识别，然后重置其他的播放状态
    }
  },
  getters: {
    getState(state) {
      return state
    }
  },
  actions: {
    setAudioObj(data: any) {
      // 设置全局的录音地址
      Object.keys(data).forEach((key: string) => {
        if ((this as any).audioObj[key] !== undefined) {
          ;(this as any).audioObj[key] = data[key]
        }
      })
    },
    notify({ title, message, path }: any) {
      getFrontCallConfig().then((res: any) => {
        const frontCallConfig = res || {}
        const showTime = (frontCallConfig.showTime || 10) * 1000
        // 获取座席工作台个人设置中浏览器配置
        if (frontCallConfig.browserAlert !== true) {
          // 没开启浏览器提醒
          return false
        }
        // 先检查浏览器是否支持
        const options: any = {
          dir: 'auto',
          lang: '',
          tag: 'call', // 部分chrome59受影响
          renotify: true,
          icon: 'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=405069346,3880007726&fm=11&gp=0.jpg',
          body: message,
          requireInteraction: true // 浏览器不自动关闭，取配置的时间，缺省的话，取10s
        }
        if (!window.Notification) {
          console.log('浏览器不支持通知')
          ElNotification({
            title,
            message,
            position: 'bottom-right',
            duration: showTime,
            onClick() {
              ;(this as any).close()
              router.push({ path: path })
            }
          })
        } else {
          // 检查用户曾经是否同意接受通知
          if (window.Notification.permission === 'granted') {
            const notification = new window.Notification(title, options) // 显示通知
            notification.onshow = function () {
              setTimeout(() => {
                console.log(frontCallConfig.showTime, '来了=======')
                notification.close.bind(notification)
              }, showTime)
            }
            notification.onclick = () => {
              window.focus()
              router.push({ path: path })
            }
          } else if (window.Notification.permission === 'default') {
            // 用户还未选择，可以询问用户是否同意发送通知
            window.Notification.requestPermission().then((permission) => {
              if (permission === 'granted') {
                new window.Notification(title, options) // 显示通知
              } else {
                // denied
                ElNotification({
                  title,
                  message,
                  position: 'bottom-right',
                  duration: showTime,
                  onClick() {
                    ;(this as any).close()
                    router.push({ path: path })
                  }
                })
                console.log('用户拒绝授权 不能显示通知')
              }
            })
          } else {
            // denied 用户拒绝
            console.log('用户曾经拒绝显示通知')
            ElNotification({
              title,
              message,
              position: 'bottom-right',
              duration: showTime,
              onClick() {
                ;(this as any).close()
                router.push({ path: path })
              }
            })
          }
        }
      })
    }
  }
})
